import api from '@/api'
import { deepCopy, setPage } from '@/utils/util'

const initPage = {
    size: 10,
    number: 0,
    total: 0,
    pages: 0,
    filter: {
        enabled: null
    },
}

const state = {
    data: {
        trainees: [],
        torgins: []
    },

    ui: {
        page: deepCopy(initPage)
    }
}

const getters = {
    trainees: state => state.data.trainees,
    torgins: state => state.data.torgins,

    traineesTotal: state => state.ui.page.total
}

// actions
const actions = {

    getTrainees({ state, commit }, params) {
        api.getTrainees({
            loading: true,
            method: 'post',
            data: params ? params : state.ui.page,
            success: (res) => {
                const { pages, total } = res
                commit('SETTRAINEEPAGE', { pages, total })

                //organizationTitle
                if (res.content && res.content.length) {
                    let trainees = res.content.map(item => {
                        if (item.organizationTitles && item.organizationTitles.length) {
                            item.organizationTitle = item.organizationTitles.join('/')
                        }
                        return item
                    })
                    commit('SETTRAINEES', {
                        trainees: trainees
                    })
                } else {
                    commit('SETTRAINEES', {
                        trainees: []
                    })
                }
            }
        })
    },

    addTrainee({ dispatch }, params) {
        api.addTrainee({
            loading: true,
            method: 'put',
            data: params,
            success: (res) => {
                dispatch('getTrainees')
            }
        })
    },

    removeTrainee({ state, dispatch }, params) {
        api.removeTrainee({
            loading: true,
            method: 'post',
            data: params,
            success: (res) => {
                dispatch('getTrainees')
            }
        })
    },

    editTrainee({ dispatch }, params) {
        api.editTrainee({
            loading: true,
            method: 'put',
            data: params,
            success: (res) => {
                dispatch('getTrainees')
            }
        })
    },

    resetTraineePassword({ dispatch }, data) {
        api.resetTraineePassword(data, {
            success: () => {
                dispatch('getTrainees')
            }
        })
    },

    getTOrgans({ commit }, params) {
        api.getOrganizations({
            method: 'get',
            data: params,
            success: (res) => {
                let torgins = res
                commit('SETTRAORGINS', { torgins })
            }
        })
    },

    updateBySelf({ commit }, params) {
        api.updateBySelf({
            method: 'put',
            data: params,
            success: (res) => {
                console.log(res)
            }
        })
    }

}

// mutations
const mutations = {

    SETTRAINEES(state, payload) {
        state.data.trainees = payload.trainees
    },

    SETTRAORGINS(state, payload) {
        state.data.torgins = payload.torgins
    },

    SETTRAINEEPAGE(state, page) {
        page && setPage(state.ui.page, page)
    },

}

export default {
    state,
    getters,
    actions,
    mutations
}
